package ext.acae2.dms.web.input;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import wt.log4j.LogR;

public class WebUtil {

    private static final Logger LOGGER = LogR.getLogger(WebUtil.class.getName());
    
    @SuppressWarnings("unchecked")
    public List<DiskFileItem> parseRequest(HttpServletRequest request) throws FileUploadException {
        LOGGER.debug("> Start parseRequest");
        
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        List<DiskFileItem> items = upload.parseRequest(request);
        
        LOGGER.debug("> End parseRequest");
        return items;
    }
    
    public String getSupplierName(List<DiskFileItem> parameters) {
        LOGGER.debug("> Start getSupplierName");
        
        for (DiskFileItem item : parameters) {
            if (item.isFormField() && "supplier".equalsIgnoreCase(item.getFieldName())) {
                return item.getString();
            }
        }
        
        LOGGER.debug("> End getSupplierName");
        return "";
    }
    
    /**
     * 获取form表单域中的值
     * @param parameters   form表单域对象
     * @param parameterName
     * @return
     */
    public String getFormFieldData(List<DiskFileItem> parameters,String parameterName) {
        LOGGER.debug("> Start getFormFieldData parameterName "+parameterName);
        
        for (DiskFileItem item : parameters) {
            if (item.isFormField() && parameterName.equalsIgnoreCase(item.getFieldName())) {
                return item.getString();
            }
        }
        
        LOGGER.debug("> End getFormFieldData");
        return "";
    }
    
}
